package com.ruoyi.purchase.service.impl;

import java.math.BigDecimal;

import com.ruoyi.base.domain.Goods;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.purchase.domain.PurchaseOrderDetail;
import com.ruoyi.purchase.dto.PurchaseOrderDto;
import com.ruoyi.purchase.mapper.PurchaseOrderDetailMapper;
import com.ruoyi.purchase.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.purchase.mapper.PurchaseOrderMapper;
import com.ruoyi.purchase.domain.PurchaseOrder;
import com.ruoyi.purchase.service.IPurchaseOrderService;

/**
 * 采购订单Service业务层处理
 *
 * @author wonderland
 * @date 2023-07-16
 */
@Service
public class PurchaseOrderServiceImpl implements IPurchaseOrderService {
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;

    @Autowired
    private PurchaseOrderDetailMapper detailMapper;

    /**
     * 查询采购订单
     *
     * @param id 采购订单主键
     * @return 采购订单
     */
    @Override
    public PurchaseOrder selectPurchaseOrderById(Long id) {
        return purchaseOrderMapper.selectPurchaseOrderById(id);
    }

    /**
     * 查询采购订单列表
     *
     * @param purchaseOrder 采购订单
     * @return 采购订单
     */
    @Override
    public List<PurchaseOrder> selectPurchaseOrderList(PurchaseOrder purchaseOrder) {
        return purchaseOrderMapper.selectPurchaseOrderList(purchaseOrder);
    }

    /**
     * 新增采购订单
     *
     * @param dto 采购订单
     * @return 结果
     */
    @Override
    public int insertPurchaseOrder(PurchaseOrderDto dto) {
        Long accountId = dto.getAccountId();
        String adminName = dto.getAdminName();
        BigDecimal depositAmount = dto.getDepositAmount();
        BigDecimal discountAmount = dto.getDiscountAmount();
        String note = dto.getNote();
        BigDecimal realAmount = dto.getRealAmount();
        String supplierName = dto.getSupplierName();
        BigDecimal totalAmount = dto.getTotalAmount();
        Long state = dto.getState();
        List<Goods> goods = dto.getGoods();
        String goodsNames = goods.stream().map(Goods::getName).collect(Collectors.joining(", "));

        PurchaseOrder purchaseOrder = insertPurchaseOrder(adminName, depositAmount, discountAmount, realAmount, note, supplierName, totalAmount, goodsNames,state);
        Long orderId = purchaseOrder.getId();
        // 创建采购订单明细记录列表
        List<PurchaseOrderDetail> purchaseOrderDetails = goods.stream()
                .map(goodsItem -> createPurchaseOrderDetail(orderId, accountId, goodsItem))
                .collect(Collectors.toList());

        // 插入采购订单明细记录
        purchaseOrderDetails.forEach(purchaseOrderDetail -> {
            int i = detailMapper.insertPurchaseOrderDetail(purchaseOrderDetail);
        });


        return 1;
    }

    private PurchaseOrderDetail createPurchaseOrderDetail(Long orderId, Long accountId, Goods goods) {
        PurchaseOrderDetail purchaseOrderDetail = new PurchaseOrderDetail();
        purchaseOrderDetail.setOrderId(orderId);
        purchaseOrderDetail.setAccountId(accountId);
        purchaseOrderDetail.setGoodsId(goods.getId());
        purchaseOrderDetail.setNumber(goods.getNumber());
        return purchaseOrderDetail;
    }

    private PurchaseOrder insertPurchaseOrder
            (String adminName, BigDecimal depositAmount,
             BigDecimal discountAmount, BigDecimal realAmount, String note,
             String supplierName, BigDecimal totalAmount, String goodsNames,Long state) {
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        purchaseOrder.setSupplierName(supplierName);
        purchaseOrder.setGoodsInfo(goodsNames);
        purchaseOrder.setAdminName(adminName);
        purchaseOrder.setTotalAmount(totalAmount);
        purchaseOrder.setDepositAmount(depositAmount);
        purchaseOrder.setNote(note);
        purchaseOrder.setReceiptNumber(OrderNoUtil.getOrderNo());
        purchaseOrder.setBillDate(new Date());
        purchaseOrder.setState(state);
        purchaseOrder.setDiscountAmount(discountAmount);
        purchaseOrder.setRealAmount(realAmount);
        purchaseOrderMapper.insertPurchaseOrder(purchaseOrder);
        return purchaseOrder;
    }

    /**
     * 修改采购订单
     *
     * @param purchaseOrder 采购订单
     * @return 结果
     */
    @Override
    public int updatePurchaseOrder(PurchaseOrder purchaseOrder) {
        return purchaseOrderMapper.updatePurchaseOrder(purchaseOrder);
    }

    /**
     * 批量删除采购订单
     *
     * @param ids 需要删除的采购订单主键
     * @return 结果
     */
    @Override
    public int deletePurchaseOrderByIds(Long[] ids) {
        return purchaseOrderMapper.deletePurchaseOrderByIds(ids);
    }

    /**
     * 删除采购订单信息
     *
     * @param id 采购订单主键
     * @return 结果
     */
    @Override
    public int deletePurchaseOrderById(Long id) {
        return purchaseOrderMapper.deletePurchaseOrderById(id);
    }

    @Override
    public int pass(Long id) {
        PurchaseOrder purchaseOrder = purchaseOrderMapper.selectPurchaseOrderById(id);
        purchaseOrder.setState(0L);
        purchaseOrderMapper.updatePurchaseOrder(purchaseOrder);
        return 1;
    }
}
